<?php
$db = new Database();
$fs = new Functions();
$set_date_time = $fs->set_default_timezone_datetime();
$auth = Auth::getAuth('current_user');
error_reporting(0);
if($_SERVER['REQUEST_METHOD'] == "POST" && isset($_POST)){
    
    if(isset($_GET['action']) && isset($_GET['type'])){
        if($_GET['action'] == "create_user" && $_GET['type'] == "add"){
            // Save Accounts
                
                $account = new Accounts($db,$auth['account_id']);
                
                $account->email = $_POST['email'];
                $account->first_name = $_POST['first_name'];
                $account->last_name = $_POST['last_name'];
                $account->department = $_POST['department_unit'];
                $account->brand_category = $_POST['brand_category'];
                $account->date_created = $set_date_time['date_time'];
                $account->date_modified = $set_date_time['date_time'];
                $account->is_active = $_POST['set_active'];
                $account->save();
                
                // Save Users
                
                $users = new Users($db,$auth['user_id']);
                
                $users->account_id = $account->id;	
                $users->username = $_POST['username'];	
                $users->password = $fs->encrypt_decrypt("encrypt", "password");	
                $users->email = $_POST['email'];	
                $users->date_created = $set_date_time['date_time'];
                $users->date_modified = $set_date_time['date_time'];
                $users->level_id = $_POST['access_role'];
                $users->is_active = $_POST['set_active'];
                $users->save();
                
                // Update Account
                $account->id = $users->account_id;
                $account->user_id = $users->id;
                $account->update();
            
                
                // Upload Photos
                if($_FILES['file']['name'] != ""){
                    $file = $_FILES['file'];
                    $filename = md5(md5($users->id));
                    $directory = "images/uploads/user_photos/" . $filename;
                    $file_path = $directory  . "/" . $filename;
                    
                    if(!is_dir($directory))
                       mkdir ($directory, 0777);
                    
                    $image_resize = new Image_Resize();
                    //$img = $image_resize->image_reduce($file,$directory,$filename);
                    
                    // Set 1 (Large)
                    $thumbnail0 = $directory."/large_".$filename.".png";
                    image_resize::create_thumbnail($file['tmp_name'],$thumbnail0,"300","300");
                    
                    
                    // Set 2 (Medium)
                    $thumbnail1 = $directory."/medium_".$filename.".png";
                    image_resize::create_thumbnail($file['tmp_name'],$thumbnail1,"150","150");
                    
                    // Set 3 (Small)
                    $thumbnail2 = $directory."/small_".$filename.".png";
                    image_resize::create_thumbnail($file['tmp_name'],$thumbnail2,"50","50");
                    
                    // Set 0 (Set Original)
                    $thumbnail = $directory."/Original_".$filename.".png";
                    //image_resize::create_thumbnail($uploadedfile,$thumbnail,$info[0],$info[1]);
                    move_uploaded_file($file['tmp_name'],$thumbnail);
                }
                
                $return = array("notification"  =>  "User was successfully created.",
                                "form_name"     =>  "#add_user");
                
                echo json_encode($return);
                
                $fs->save_audit_logs($db,$users->id,"6","tb_users",$set_date_time['date_time'],$users->id);
                
        } elseif($_GET['action'] == "update_user" && $_GET['type'] == "update"){
            // Update Accounts
            
                $user_id = $_POST['user_id'];

                $users = new Users($db,$user_id);
                $users->id = $user_id;	
                //$users->account_id = $account->id;	
                $users->username = $_POST['username'];	
                $users->email = $_POST['email'];	
                $users->date_modified = $set_date_time['date_time'];
                $users->level_id = $_POST['access_role'];
                $users->is_active = $_POST['set_active'];
                $users->update();
            
                
                $account = new Accounts($db,$users->account_id);
                
                $account->id = $users->account_id;
                $account->email = $_POST['email'];
                $account->first_name = $_POST['first_name'];
                $account->last_name = $_POST['last_name'];
                $account->department = $_POST['department_unit'];
                $account->brand_category = $_POST['brand_category'];
                $account->date_modified = $set_date_time['date_time'];
                $account->is_active = $_POST['set_active'];
                $account->update();
                
                
    
                // Upload Photos
                if($_FILES['file']['name'] != ""){
                    $file = $_FILES['file'];
                    $filename = md5(md5($users->id));
                    $directory = "images/uploads/user_photos/" . $filename;
                    $file_path = $directory  . "/" . $filename;
                    
                    if(!is_dir($directory))
                       mkdir ($directory, 0777);
                    
                    $image_resize = new Image_Resize();
                    //$img = $image_resize->image_reduce($file,$directory,$filename);
                    
                    // Set 1 (Large)
                    $thumbnail0 = $directory."/large_".$filename.".png";
                    image_resize::create_thumbnail($file['tmp_name'],$thumbnail0,"300","300");
                    
                    
                    // Set 2 (Medium)
                    $thumbnail1 = $directory."/medium_".$filename.".png";
                    image_resize::create_thumbnail($file['tmp_name'],$thumbnail1,"150","150");
                    
                    // Set 3 (Small)
                    $thumbnail2 = $directory."/small_".$filename.".png";
                    image_resize::create_thumbnail($file['tmp_name'],$thumbnail2,"50","50");
                    
                    // Set 0 (Set Original)
                    $thumbnail = $directory."/Original_".$filename.".png";
                    //image_resize::create_thumbnail($uploadedfile,$thumbnail,$info[0],$info[1]);
                    move_uploaded_file($file['tmp_name'],$thumbnail);
                }
                
                $return = array("notification"  =>  "Done.",
                                "msg"           =>  "User was successfully updated.",
                                "form_name"     =>  "#update_user","asd"=>$thumbnail1);
                
                echo json_encode($return);
                
                $fs->save_audit_logs($db,$user_id,"4","tb_users",$set_date_time['date_time'],$user_id);
        }
    }else{
        $this->setLayout();
    }
    
    
    
}else{
    $this->setLayout();
}
?>